﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FwProject.Authorization.Users;
using FwProject.Com;
using FwProject.Com.Dto;
using FwProject.Commodity.Dto;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Abp.Authorization;
using FwProject.Configuration;
using Abp.UI;
namespace FwProject.Controllers
{
    [Route("api/[controller]/[action]")]
    [AbpAuthorize]
    public class AppFeedbackController : FwProjectControllerBase
    {
        private readonly IHostingEnvironment env;
        private readonly IConfigurationRoot _appConfiguration;
        private readonly IFeedbackAppService _feedbackAppService;
        public AppFeedbackController(IHostingEnvironment _env,  IFeedbackAppService feedbackAppService)
        {
            env = _env;
            _appConfiguration = _env.GetAppConfiguration(); ;
            _feedbackAppService = feedbackAppService;
        }
        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="input"></param>
        /// <param name="image"></param>
        /// <returns></returns>
        [HttpPost]
        public async  Task<IActionResult> AddFeedback(FeedbackInput input, IFormCollection image)
        {
            Logger.Debug("测试上传");

            var model = new User();
            string path = string.Empty;
          
            var files = image.Files;
         
            //格式限制
            var allowType = new string[] { "image/jpg", "image/png", "image/jpeg", };
            List<string> ImagePath = new List<string>();
            if (files.Count > 0) {
                if (files.Any(c => allowType.Contains(c.ContentType)))
                {
                    try
                    {
                        if (files.Sum(c => c.Length) <= 1024 * 1024 * 4)
                        {
                            foreach (var item in files)
                            {
                                var file = item;
                                string strpath = DateTime.Now.ToString("yyyyMMddHHmmss")+Guid.NewGuid() + Path.GetExtension(file.FileName);// Path.Combine("Feedback", DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(file.FileName));
                                //foreach (var file in files)
                                //{
                                //  string strpath = Path.Combine("Upload", DateTime.Now.ToString("MMddHHmmss") + file.FileName);
                                path = Path.Combine(env.WebRootPath, strpath);
                                var tempPath = Path.Combine(env.WebRootPath, "Feedback");
                                if (!Directory.Exists(tempPath))
                                    Directory.CreateDirectory(tempPath);

                                Logger.Debug($"{tempPath}======={env.WebRootPath}");
                                Logger.Debug(path);


                                string FileServicePath = _appConfiguration["FileServicePath:Feedback:SavaFilePath"].ToString();

                                if (!Directory.Exists(FileServicePath))
                                    Directory.CreateDirectory(FileServicePath);

                                using (var stream = new FileStream(Path.Combine(FileServicePath, strpath), FileMode.OpenOrCreate, FileAccess.ReadWrite))
                                {
                                    await file.CopyToAsync(stream);
                                }

                                ImagePath.Add($"{_appConfiguration["FileServicePath:Feedback:Virtual"].ToString()}{strpath}");

                            }
                            input.Imgs = string.Join('|', ImagePath.ToArray());

                        }
                        else
                        {

                          throw  new UserFriendlyException("图片过大限制4M");
                        }
                    }
                    catch (Exception exe)
                    {

                        Logger.Error(exe.ToString());
                        throw new UserFriendlyException("请求错误");
                    }


                }
                else
                {
                    throw new UserFriendlyException("图片格式错误");
                 
                }
            }
            await _feedbackAppService.CreateFeedback(input);
            return Json("");
        }
    }
}
